package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.utils.ThreadLocalUtil;
import com.itheima.reggie.web.R;
import com.itheima.reggie.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理控制器
 *
 * @Name : AddressBookController
 * @Author : WuQiang
 * @Date : 2022-08-20 17:17
 * @Description : 地址管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 根据用户查询全部地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R getList(AddressBook addressBook) {
        Long userId = ThreadLocalUtil.getCurrentId();
        addressBook.setUserId(userId);
        List<AddressBook> addressBookList = addressBookService.list(Wrappers.lambdaQuery(AddressBook.class)
                .eq(userId != null, AddressBook::getUserId, userId));
        return R.success("查询地址成功", addressBookList);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R save(@RequestBody AddressBook addressBook) {
        return addressBookService.addAddressBook(addressBook);
    }

    /**
     * 根据地址id 查询回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        //参数校验
        if (ObjectUtils.isEmpty(id)) {
            throw new BusinessException("参数非法.");
        }
        AddressBook addressBook = addressBookService.getById(id);
        return R.success("查询成功", addressBook);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R update(@RequestBody AddressBook addressBook) {
        //todo 参数校验
        boolean result = addressBookService.updateById(addressBook);
        if (result) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 设置默认地址
     *
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        //先根据用户id默认地址，并将其设置为普通地址
        addressBookService.update(Wrappers.lambdaUpdate(AddressBook.class)
                .eq(AddressBook::getUserId, ThreadLocalUtil.getCurrentId())
                .set(AddressBook::getIsDefault, 0));

        //将地址设置成默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");

    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, ThreadLocalUtil.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success("", addressBook);
        }
    }

    @DeleteMapping
    public R delete(Long ids){
        boolean result = addressBookService.removeById(ids);
        if (result) {
            return R.success("删除地址成功");
        }
        return R.error("删除地址失败");
    }
}
